package com.zxh.blog.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxh.blog.domain.Comment;
import com.zxh.blog.service.CommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class AdminCommentController {
	@Autowired
	CommentService commentService;

	@RequiresPermissions("comment:read")
	@RequestMapping(value = "/index",method = RequestMethod.GET)
	public String index(){
		return "/comment/index.jsp";
	}

	@RequestMapping(value = "/create",method = RequestMethod.POST)
	@ResponseBody
	public Map create(Comment comment){
		Date date = new Date();
//		permission.setCreate_time(date.toString());
//		permission.setUpdate_time(date.toString());
		Integer integer = commentService.addComment(comment);
		HashMap map = new HashMap();
		if(integer == 1){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}

//	@RequiresPermissions("role:add")
//	@RequestMapping(value = "/update",method = RequestMethod.POST)
//	@ResponseBody
//	public Map update(Comment comment){
//		Date date = new Date();
////		permission.setUpdate_time(date.toString());
//		commentService.updateCommCent(comment);
//		HashMap map = new HashMap();
//		if(true){
//			map.put("result","success");
//		}else{
//			map.put("result","fail");
//		}
//		return map;
//	}

	@RequiresPermissions("comment:delete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Map delete(@PathVariable("ids") int[] ids){
		commentService.batchDelete(ids);
		HashMap map = new HashMap();
		if(true){
			map.put("result","success");
		}else{
			map.put("result","fail");
		}
		return map;
	}

	//@RequestParam(required = false, defaultValue = "0", value = "offset")
	//@RequestParam(required = false, defaultValue = "10", value = "limit")
	@RequiresPermissions("comment:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map list(@RequestParam int offset, @RequestParam int limit) {
		int a = (offset/limit)+1;
		PageHelper.startPage(a,limit);
		List<Comment> commentList = commentService.selectByList();
		PageInfo<Comment> p=new PageInfo<Comment>(commentList);
		Map<String, Object> result = new HashMap();
		result.put("rows", p.getList());
		result.put("total", p.getTotal());
		return result;
	}
}
